package org.demo.algorithm.leetcode.linked;

import org.demo.algorithm.common.ListNode;

/**
 * 141
 */
public class HasCycle {

    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head.next;

        while (fast != slow) {
            if (fast == null || fast.next == null){
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }

    public static void main(String[] args) {
        HasCycle hasCycle = new HasCycle();
        boolean b = hasCycle.hasCycle(getList());
        System.out.println(b);
    }

    public static ListNode getList() {
        ListNode l1 = new ListNode(3);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(0);
        ListNode l4 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4.next;
        l4.next = l2;
        return l1;
    }

}
